package au.org.nh.dao.mongo.impl;

import java.util.List;

import au.org.nh.dao.PatientInfo;
import au.org.nh.dao.PatientInfoDAO;

import com.google.gson.Gson;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.util.JSON;

public class SavePatientInfoMongoImpl implements PatientInfoDAO {
	DBCollection testCollection = null;

	@Override
	public void save(final PatientInfo info) throws DBException {
		System.out.println("[ENTER] SavePatientInfoMongoImpl ");
		final Gson gson = new Gson();
		final DBObject patientInfo = (DBObject) JSON.parse(gson.toJson(info));
		try {
			System.out.println("[BEFORE] getCollection ");
			testCollection = MongoDBUtil.getCollection("patient_info");
			if (testCollection == null) {
				System.out.println("[ERROR] getCollection returned null ");
			} else {
				System.out
						.println("[SUCCESS] getCollection returned non null ");
			}
			System.out.println("[AFTER] getCollection ");
		} catch (final MongoDBException e) {
			e.printStackTrace();
			throw e;
		}
		try {
			System.out.println("[BEFORE] insert ");
			testCollection.save(patientInfo, WriteConcern.SAFE);
			System.out.println("[AFTER] insert ");
		} catch (final MongoException e) {
			e.printStackTrace();
			System.out.println("[EXITING] Exception has been caught "
					+ e.getMessage());
			throw new MongoDBException(e.getMessage());
		}
		System.out.println("[EXITING] save ");
	}

	@Override
	public PatientInfo get(final String name) throws DBException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<PatientInfo> getAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PatientInfo getById(final Long Id) throws DBException {
		// TODO Auto-generated method stub
		return null;
	}

}
